from common.utils.rights import permission_required, view_logging_required
from common.utils.file_read import file_read
from . import index_bp
from flask import render_template,request
from flask_wtf.form import CombinedMultiDict
from models.form import UploadForm
import cv2


@index_bp.get('/back_pro')
@index_bp.post('/back_pro')
@view_logging_required
@permission_required("back:pro")
def back_pro():
    if request.method == "GET":
        return render_template('admin/cv/back.html')
    else:
        form = UploadForm(CombinedMultiDict([request.form, request.files]))
        if form.validate():
            f = request.files['file']
            file_url = file_read(f)
            img = cv2.imread(file_url, 1)
            img_shape = img.shape
            h = img_shape[0]
            w = img_shape[1]
            # 彩色图像转换为灰度图像（3通道变为1通道）
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # 最大图像灰度值减去原图像，即可得到反转的图像
            dst = 255 - gray
            res_pic = "static/download/back.jpg"
            cv2.imwrite(res_pic,dst)
            return render_template('admin/cv/back.html', pic=file_url, pic_res=res_pic)
        else:
            return "请传入正确的文件格式"

